package com.hmall.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;


@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> pageItem(Integer page, Integer size) {
        Page<Item> selectPage = new Page<>(page, size);
        Page<Item> itemPage = itemMapper.selectPage(selectPage, null);
        long total = itemPage.getTotal();
        List<Item> records = itemPage.getRecords();
        return new PageDTO<>(total, records);
    }

    /**
     * 新增商品
     *
     * @param item
     * @return
     */
    @Override
    public void itemAdd(Item item) {
        Item items = new Item();
        //将item属性复制给items中
        BeanUtils.copyProperties(item, items);
        //获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        //将localdatetime格式转化为date格式
        LocalDate localDate = localDateTime.toLocalDate();
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        items.setCreateTime(date);
        items.setUpdateTime(date);
        //新增商品
        itemMapper.insert(items);
    }

    /**
     * 商品上架，下架
     *
     * @param id
     * @param status
     */
    @Override
    @Transactional
    public void itemStatus(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        //声明交换机
        String exchangeName = "item.direct";
        //消息
        String meassge = status == 1 ? "item.upstatus" : "item.downstatus";
        //发送消息
        rabbitTemplate.convertAndSend(exchangeName, meassge, id);

        itemMapper.updateById(item);
    }

    /**
     * 修改商品信息
     *
     * @param item
     */
    @Override
    public void itemUpdate(Item item) {
        Item itemlist = new Item();
        //条件查询 查询id
        LambdaUpdateWrapper<Item> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Item::getId, item.getId());
        //将item属性赋值给itemlist
        BeanUtils.copyProperties(item, itemlist);
        //获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        //将localdatetime格式转化为date格式
        LocalDate localDate = localDateTime.toLocalDate();
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        itemlist.setUpdateTime(date);
        itemMapper.update(itemlist, updateWrapper);
    }

    @Transactional
    @Override
    public void updateStock(Long itemId, Integer num) {
      /*  //查询，立即获取互斥锁
        Item item = getById(itemId);
        //条件构造器 mybatispules
        try {
            if(item.getStock()>=num){
                LambdaQueryWrapper<Item> QueryWrapper = new LambdaQueryWrapper<>();
                //类似于sql语句中的where后面的语句
                QueryWrapper.eq(Item::getId,itemId);
                Item it = new Item();
                it.setStock(item.getStock()-num);
                itemMapper.update(it,QueryWrapper);
        }
        }catch (Exception e){
            throw new RuntimeException("库存不足");
        }*/
        try {
            update().setSql("stock = stock-" + num).eq("id", itemId).update();
        } catch (Exception e) {
            throw new RuntimeException("库存不足");
        }
    }
}